// pages/projects/detail/detail.js
var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    swiperinfo: {
      hgt: 0,
      wtd: 0,
      indicatorDots: true,
      autoplay: true,
      circular: true,
      interval: 5000,
      duration: 500
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    WxParse.emojisInit('[]', "/wxParse/emojis/", {});

    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var swiper_wtd = res.windowWidth;
        var swiper_hgt = 480 * swiper_wtd / 1000;
        var swiperinfo = that.data["swiperinfo"];
        swiperinfo.hgt = swiper_hgt;
        swiperinfo.wtd = swiper_wtd;
        that.setData({
          swiperinfo: swiperinfo
        });
      },
    });
    this.setData({
      cntflag: '1'
    });
    console.log('goodsid=' + options.goodsid);

    this.loadGoodsToServer(options.goodsid);
  },
  loadGoodsToServer: function (goodsid) {
    var url = app.globalData.webUrl + 'Goods/goodsmsg';

    var tdata = { goodsid: goodsid };

    app.loadDataFromServer(url, this.loadCallBack, tdata);
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../../imgs/tips.png'
      });
      return;
    }

    var goods = res.result.ginfo;
    console.log(goods);
    var banners = res.result.imgarr;
    for (var i = 0; i < banners.length; i++) {
      banners[i].imgurl = app.globalData.ImgwebUrl + banners[i].iconurl;
    }
    // var imgs = app.repicSrc(goods.htmlmsg);
    goods.banners = banners;
    // goods.details = imgs;
    goods.att = res.result.att;
    // this.data.goods = goods;
    this.setData({
      goods: goods
    });
    var article = goods.htmlmsg;
    article = article.replace(/&nbsp;/g, '');
    article = article.replace(/amp;/g, '');
    WxParse.wxParse('article', 'html', article, this, 5);
  },
  payment: function () {

    var url = app.globalData.webUrl + 'Payment/pay_code';
    var saveObject = { goodsid: this.data.goods.id, num: 1 };
    app.loadDataFromServer(url, this.PayCallBack, saveObject);
  },
  PayCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: res.msg,
        icon: "none",
        duration: 3000
      });

      if (res.status == '-2') {
        wx.navigateTo({
          url: '../../my/phone/bindphone',
        })
      }
      return;
    }

    var jsapi = res.result;
    wx.requestPayment({
      'appId': jsapi.appId,
      'timeStamp': jsapi.timeStamp,
      'nonceStr': jsapi.nonceStr,
      'package': jsapi.package,
      'signType': 'MD5',
      'paySign': jsapi.paySign,
      'success': function (res) {
        wx.showToast({
          title: '支付成功',
          duration: 1000
        });

      },
      'fail': function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  buyGoods: function () {
    //购买商品
    this.payment();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    // var that = this;
    // var storeid = app.globalData.storeid;
    // var gid = this.data.goods.id;
    // // 设置菜单中的转发按钮触发转发事件时的转发内容
    // var shareObj = {
    //   title: this.data.goods.gname,        // 默认是小程序的名称(可以写slogan等)
    //   path: '/pages/projects/detail/detail?goodsid=' + gid + "storeid=" + storeid,        // 默认是当前页面，必须是以‘/’开头的整路径
    //   imgUrl: '',     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
    // };

    // // 返回shareObj
    // return shareObj;

    return app.getShareObj();
  },
  swpierChange: function (e) {
    var index = e.detail.current;
    this.setData({
      swiperCurrent: index
    });
  },
  changeFlag: function (e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      cntflag: flag
    });
  }
})